'use strict';

const BaseController = require('./base');
const qiniu = require('../../utils/qiniu');
const await = require('await-stream-ready/lib/await');
const LogicErrException = require('../../exception/logicError');

class UserController extends BaseController {

  async login() {
    const { ctx } = this;
    const params = ctx.request.query;
    const result = await ctx.service.sys.user.login(params.username, params.password);
    this.success(result);
  }

  // 添加用户
  async addUser() {
    const { ctx } = this;
    let result = await ctx.service.sys.user.add();
    this.success(result);
  }

  // 通过Id获取用户信息
  async getInfo() {
    const { ctx, app } = this;
    const token = ctx.query.token;
    if (token === 'undefined') {
      return;
    }
    let tokenData = ctx.app.jwt.verify(ctx.query.token, app.config.jwt.secret);//验证token
    const result = await ctx.service.sys.user.getInfo(tokenData.userId);
    this.success(result);
  }

  // 分页获取用户
  async getUsers() {
    const { ctx } = this;
    const result = await ctx.service.sys.user.getUsers();
    this.success(result);
  }

  // 删除菜单
  async deleteUser() {
    const { ctx } = this;
    const result = await ctx.service.sys.user.deleteUser(ctx.query.id);
    this.success(result);
  }

  // 更新用户 
  async updateUser() {
    const { ctx } = this;
    let result = await ctx.service.sys.user.updateUser(ctx.request.body);
    this.success(result);
  }

  // 添加权限     
  async updateAuthority() {
    const { ctx } = this;
    const postData = ctx.request.body;
    const result = await ctx.service.sys.user.updateAuthority(postData);
    this.success(result);
  }

  // 获取用户权限
  async getUserAuthority() {
    const { ctx } = this;
    const result = await ctx.service.sys.user.getUserAuthority(ctx.query.userId);
    this.success(result);
  }

}

module.exports = UserController;
